﻿// ADSL.cpp : 实现文件
//

#include "stdafx.h"
#include "AdslMgr.h"
#include "MobileGameDlg.h"

// CADSL

IMPLEMENT_DYNAMIC(CADSL, CWnd)

CADSL::CADSL()
{
	sRetInfo = _T("");
	sAdslName = _T("");
	sAdslIp = _T("");
}

CADSL::~CADSL()
{
}


BEGIN_MESSAGE_MAP(CADSL, CWnd)
END_MESSAGE_MAP()

void CADSL::OnEnum(CStringArray& arr) {
	RASENTRYNAME Entries[15];
	DWORD cb, cEntries, dwRet;
	Entries[0].dwSize = sizeof(RASENTRYNAME);
	cb = sizeof(Entries);
	cEntries = 0;

	dwRet = ::RasEnumEntriesW(NULL, NULL, &Entries[0], &cb, &cEntries);  //如果函数成功，则返回0

	if (dwRet == 0)
	{
		//m_list.ResetContent();

		for (UINT i = 0; i < cEntries; i++)
			arr.Add(Entries[i].szEntryName);
		

		//m_list.SetCurSel(0);
	}
}
void CADSL::OnEnum()
{

	RASENTRYNAME Entries[15];
	DWORD cb, cEntries, dwRet;
	Entries[0].dwSize = sizeof(RASENTRYNAME);
	cb = sizeof(Entries);
	cEntries = 0;

	dwRet = ::RasEnumEntriesW(NULL, NULL, &Entries[0], &cb, &cEntries);  //如果函数成功，则返回0

	if (dwRet == 0)
	{
		//m_list.ResetContent();

		for (UINT i = 0; i<cEntries; i++)
			sAdslName = Entries[i].szEntryName;

		//m_list.SetCurSel(0);
	}
}

UINT CADSL::CheckNet()
{
	DWORD dwFlag;
	if (!InternetGetConnectedState(&dwFlag, 0))
	{
		//MessageBox("网络未连接。");
		//SetDlgItemText(IDC_STATIC_NETTYPE, _T("未连接到网络"));
		return 0;

	}
	else if (dwFlag & INTERNET_CONNECTION_MODEM)
	{
		//SetDlgItemText(IDC_STATIC_NETTYPE, _T("上网类型：采用RAS拨号连接上网"));
		return 1;
	}
	else  if (dwFlag & INTERNET_CONNECTION_LAN)
	{
		//SetDlgItemText(IDC_STATIC_NETTYPE, _T("上网类型：采用网卡通过局域网上网"));
		return 2;
	}
	else if (dwFlag & INTERNET_CONNECTION_PROXY)
	{
		//SetDlgItemText(IDC_STATIC_NETTYPE, _T("上网类型：使用代理服务器上网"));
		return 3;
	}
	return 0;
}

BOOL CADSL::GetConnectState()
{
	LPRASCONN lpRasConn = NULL;
	DWORD cbBuf = 0;
	DWORD cConn = 0;
	DWORD dwRet = 0;
	HRASCONN hrasconn;
	RASCONNSTATUS rasStatus;
	UINT ndx;

	// enumerate connections 
	cbBuf = sizeof(RASCONN);

	if ((lpRasConn = (LPRASCONN)malloc((UINT)cbBuf)) != NULL)
	{
		lpRasConn->dwSize = sizeof(RASCONN);
		dwRet = RasEnumConnections(lpRasConn, &cbBuf, &cConn);
		for (ndx = 0; ndx < cConn; ndx++)
		{
			// get to HRASCONN 
			hrasconn = lpRasConn[ndx].hrasconn;
			// get connection status 
			rasStatus.dwSize = sizeof(RASCONNSTATUS);
			dwRet = RasGetConnectStatus(hrasconn, &rasStatus);
			if (dwRet)
				return 0;
			else
			{
				if (rasStatus.rasconnstate == RASCS_Connected)
					return 1;
				else return 0;
			}
		}
	}
	else
	{
		return 0;
	}

}

void CADSL::GetAdslStyle()
{
	RASCONN ras[2];
	ras[0].dwSize = sizeof(RASCONN);
	DWORD dSize, dNumber, dCount = 0L;
	dSize = sizeof(ras);

	if (::RasEnumConnections(ras, &dSize, &dNumber) == 0)   //如果函数成功，则返回0
	{
		if (dNumber>0)
		{
			RASPPPIP	raspppip;						//获取拨号连接的IP地址
			raspppip.dwSize = sizeof(RASPPPIP);
			DWORD cb = sizeof(RASPPPIP);
			::RasGetProjectionInfo(ras[0].hrasconn, RASP_PppIp, &raspppip, &cb);
			//			AfxMessageBox("IP地址: " + (CString)raspppip.szIpAddress );		//IP地址	
			sAdslIp = (CString)raspppip.szIpAddress;

			/*
			AfxMessageBox("设备类型: " + (CString)ras[0].szDeviceType);		//设备类型
			AfxMessageBox("设备名称: " + (CString)ras[0].szDeviceName);		//设备名称
			AfxMessageBox("连接名称: " + (CString)ras[0].szEntryName);		//连接名称
			*/
			// 			SetDlgItemText(IDC_STATIC_IP, (CString)raspppip.szIpAddress);
			// 			SetDlgItemText(IDC_STATIC2,"设备类型: " + (CString)ras[0].szDeviceType);
			// 			SetDlgItemText(IDC_STATIC3,"设备名称: " + (CString)ras[0].szDeviceName);
			//SetDlgItemText(IDC_STATIC4,"连接名称: " + (CString)ras[0].szEntryName);

		}
	}
}


void CADSL::RAS_Close()   		//关闭当前使用中的拨号连接
{

	m_bClosing = true;
	sRetInfo = _T("正在关闭当前的网络连接，请稍候...");

	RASCONN ras[20];
	DWORD dwSize, dwNumber, dwCount;
	ras[0].dwSize = sizeof(RASCONN);
	dwSize = sizeof(ras);

	if (::RasEnumConnections(ras, &dwSize, &dwNumber) == 0)
	{
		for (dwCount = 0; dwCount<dwNumber; dwCount++)   	//依次关闭所有的连接
		{
			if (::RasHangUp(ras[dwCount].hrasconn) != 0)		//如果函数成功，则返回0
			{
				break;
			}
		}
	}

	//	SetDlgItemText(IDC_STATIC_IP,"");
	//			SetDlgItemText(IDC_STATIC2,"");
	//			SetDlgItemText(IDC_STATIC3,"");
	//			SetDlgItemText(IDC_STATIC4,"");
	sRetInfo = _T("网络连接已经关闭!");

	m_bConnected = false;
	m_bClosing = false;

	if (::IsWindowVisible(GetSafeHwnd()))
	{
		this->CheckNet();
	}
}



UINT CADSL::RAS_Connect(TCHAR *szUserName, TCHAR *szPassword, TCHAR *strEntryName)
{
	m_bConnecting = true;	//正在连接，不能关闭程序

	sRetInfo = _T("正在连接拨号网络,请稍候...");

	// 	CString strEntryName;
	// 	m_list.GetText(m_list.GetCurSel(),strEntryName);
	// 	TCHAR szUserName[MAX_PATH], szPassword[MAX_PATH];
	// 	GetDlgItemText(IDC_USER,szUserName,MAX_PATH);
	// 	GetDlgItemText(IDC_PASS,szPassword,MAX_PATH);


	HRASCONN hRasConn = NULL;

	RASDIALPARAMS rdParams;							//拨号连接的信息
	ZeroMemory(&rdParams, sizeof(RASDIALPARAMS));
	rdParams.dwSize = sizeof(RASDIALPARAMS);
	lstrcpy(rdParams.szEntryName, strEntryName);
	lstrcpy(rdParams.szUserName, szUserName);
	lstrcpy(rdParams.szPassword, szPassword);


	DWORD dwRet;
	dwRet = ::RasDial(NULL, NULL, &rdParams, 0L, NULL, &hRasConn);  //如果函数成功，则返回0
	if (dwRet)
	{
		TCHAR  szBuf[256] = _T("");
		::RasGetErrorString((UINT)dwRet, (LPWSTR)szBuf, 256);
		sRetInfo = _T("连接错误! ") + CString(szBuf);
		/*AfxBeginThread(sndError,this);*/
		m_bConnecting = false;
		WinExec((LPCSTR)_T("rasphone -a"), SW_SHOW);
		return 0;
	}

	/*----------------------------------------------------------------------*/
	RASPPPIP	raspppip;						//获取拨号连接的IP地址
	raspppip.dwSize = sizeof(RASPPPIP);
	DWORD cb = sizeof(RASPPPIP);
	::RasGetProjectionInfo(hRasConn, RASP_PppIp, &raspppip, &cb);

	sAdslIp = (CString)raspppip.szIpAddress;
	// 	SetDlgItemText(IDC_STATIC_IP,m_strIP);

	// 	char szTimes[20];
	// 	itoa(m_nTimes,szTimes,10);
	// 	m_list2.InsertItem(0,szTimes);
	// 	m_list2.SetItemText(0,1,m_strIP);	
	// 	CTime tt=CTime::GetCurrentTime();
	// 	m_list2.SetItemText(0,2,tt.Format("%H:%M:%S"));	
	// 
	// 	m_nTimes++;

	/*----------------------------------------------------------------------*/


	sRetInfo = _T("已经成功地建立了连接");
	m_bConnected = true;
	m_bConnecting = false;

	if (::IsWindowVisible(GetSafeHwnd()))
	{
		this->CheckNet();
	}
	return 1;
}
// CADSL 消息处理程序


